﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;
using PrimaryKeyFunction;

public partial class Admin_ChooseExperiment : System.Web.UI.Page
{
    Service objService = new Service();
    Functions objFunction = new Functions();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            fillDataGrid();
        }
    }

    protected void btnNavigate_Click(object sender, EventArgs e)
    {
        if (txtExpId.Text == string.Empty)
        {
            lblMessage.Text = "Please select the Experiment";
        }
        else
        {
            Session["ExperimentId"] = txtExpId.Text;
            Session["YieldId"] = objFunction.GetExpYieldId(txtExpId.Text);
            Session["LiquidCompId"] = objFunction.ExpGetLiquidComId(txtExpId.Text);
            Response.Redirect("Material.aspx");
        }
    }

    private void fillDataGrid()
    {
        string RefeId = (string)Session["ReferenceId"];
        if (objService.GetExperiment(RefeId).Tables[0].Rows.Count < 1)
        {
            btnAddExperiment.Visible = true;
            lblMessage.Text = "You don't have the Experiments for this reference,please click over the add experiment button";
            btnNavigate.Visible = false;
            trChooseExpTxtBox.Visible = false;
        }
        else
        {
            dgExperiment.DataSource = objService.GetExperiment(RefeId).Tables[0];
            dgExperiment.DataBind();
        }
    }

    protected void dgExperiment_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgExperiment.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillExperimentDetails(id);
    }

    private void fillExperimentDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetExperimentDetailsBasedonPK(id);
        txtSummary.Text = ds.Tables[0].Rows[0]["exp_summary"].ToString();
        txtExpId.Text = ds.Tables[0].Rows[0]["experiment_id"].ToString();
        txtExperimentName.Text = ds.Tables[0].Rows[0]["ExperimentName"].ToString();
    }
    protected void btnAddExperiment_Click(object sender, EventArgs e)
    {
        Response.Redirect("Experiment.aspx");
    }
    protected void dgExperiment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgExperiment.PageIndex = e.NewPageIndex;
        fillDataGrid();
    }
}
